const SERVICE = require("../../lib/js/service.js");
const Golbal = require("../../lib/js/global.js");

Page({
  data:{
    isFocus: false,   //input框是否聚焦
    tempFilePaths: [],    //图片路径 (Array)
    /*租值信息 start*/
      TypeVal: "",   //类型
      province: "",    //省
      city: "",    //市
      county: "",    //县
      RentWayVal: "",    //出租方式
    certification: ["未认证", "个体认证", "企业认证", "4S店认证", "审核中 ...", "审核不通过"]  //认证
    /*租值信息 end*/
    
  },

  onLoad(options){
    if(options.detailId){
      this.setData({
        detailId: options.detailId
      });
      this.MagdebrugDetail(options.detailId)
    }

    let _this = this;
    wx.showNavigationBarLoading();

    SERVICE.POST(SERVICE.URL.userInfo, {
      thirdCookie: wx.getStorageSync("thirdCookie")
    },(status, res) => {
      wx.hideNavigationBarLoading();
      if (res.data.status == "success") {
        _this.setData({
          userInfo: res.data.user_info
        });
      } else {
        wx.showToast({
          title: "获取用户信息失败",
          icon: "none",
          mask: true
        });
      }
    });
    
  },

  /*分享*/
  onShareAppMessage() { },


  /*标题 / 聚焦*/
  titleInput() {
    this.setData({
      isFocus: true
    });
  },
  
  getMobile(e){
    let mobile = e.detail.value;
    if (mobile.length > 0 && Golbal.checkPhone(mobile) === false) {
      wx.showToast({
        title: "请输入正确的手机号",
        icon: "none",
        mask: true
      })
    }
  },

  /*租值消息 start*/
  /*类型*/
  RentType() {
    let _this = this, type_item = ["临街门面", "市场门面", "停车场"]
    wx.showActionSheet({
      itemList: type_item,
      success(res) {
        _this.setData({
          TypeVal: type_item[res.tapIndex]
        });
      },
      fail() {
        console.log("我取消类型了");
        _this.setData({
          TypeVal: ""
        });
      }
    })
  },

  /*位置*/
  RegionChange(e) {
    let location = e.detail.value;
    this.setData({
      province: location[0],
      city: location[1],
      county: location[2]
    });
  },

  /*出租方式*/
  RentWay() {
    let _this = this, RentWay_item = ["月租", "年租", "不限"]
    wx.showActionSheet({
      itemList: RentWay_item,
      success(res) {
        _this.setData({
          RentWayVal: RentWay_item[res.tapIndex]
        });
      },
      fail() {
        console.log("取消出租方式");
        _this.setData({
          RentWayVal: ""
        });
      }
    })
  },

  /*发布*/
  formSubmit(e) {
    let td = this.data, form_obj = e.detail.value;

    if (!form_obj.title) {
      wx.showToast({
        title: "请输入标题",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.type) {
      wx.showToast({
        title: "请选择类型",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.location) {
      wx.showToast({
        title: "请选择位置",
        icon: "none",
        mask: true
      });
    }else if (!form_obj.contact_name) {
      wx.showToast({
        title: "请输入联系人",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.contact) {
      wx.showToast({
        title: "请输入电话",
        icon: "none",
        mask: true
      });
    } else if (form_obj.contact.length > 0 && Golbal.checkPhone(form_obj.contact) === false) {
      wx.showToast({
        title: "请输入正确的手机号",
        icon: "none",
        mask: true
      })
    } else if (form_obj.checkbox.length == 0) {
      wx.showToast({
        title: "未同意协议",
        icon: "none",
        mask: true
      });
    } else {
     if(td.detailId){
       this.saveDemandInfo(form_obj);
     }else{
       this.publish(form_obj);
     }
    }
  },

  MagdebrugDetail(id){
    let _this = this;
    SERVICE.POST(SERVICE.URL.MagdebrugDetail,{
      thirdCookie: wx.getStorageSync("thirdCookie"),
      demand_id: id
    },(status,res)=>{
      _this.setData({
        detail_info: res.data,
        province: res.data.rental_list.province,
        city: res.data.rental_list.city,
        county: res.data.rental_list.county
      });
    });
  },

  publish(formObj){
    let td = this.data;
    wx.showLoading({
      title: "发布中...",
      mask: true
    });
    SERVICE.POST(SERVICE.URL.rent, {
      thirdCookie: wx.getStorageSync("thirdCookie"),
      title: formObj.title,
      detail_content: formObj.textarea,
      genre: formObj.type,
      province: td.province,
      city: td.city,
      county: td.county,
      contact_name: formObj.contact_name,
      contact_num: formObj.contact,
      protocol_status: formObj.checkbox.length == 0 ? 0 : 1
    }, (status, res) => {
      wx.hideLoading();
      console.log("发布求租：", res);
      if (res.data.status == "success") {
        wx.showToast({
          title: "发布成功",
          icon: "none",
          mask: true,
          duration: 2000,
          success: function () {
            wx.redirectTo({
              url: "../car-rental/car-rental",
            });
          }
        });
      } else {
        wx.showToast({
          title: "发布失败",
          icon: "none",
          mask: true,
          duration: 2000
        });
      }
    });
  },

  saveDemandInfo(formObj){
    let td = this.data;    
    wx.showLoading({
      title: "编辑中...",
      mask: true
    });
    SERVICE.POST(SERVICE.URL.saveDemandInfo, {
      thirdCookie: wx.getStorageSync("thirdCookie"),
      title: formObj.title,
      detail_content: formObj.textarea,
      genre: formObj.type,
      province: td.province,
      city: td.city,
      county: td.county,
      contact_name: formObj.contact_name,
      contact_num: formObj.contact,
      protocol_status: formObj.checkbox.length == 0 ? 0 : 1,
      id: td.detailId
    }, (status, res) => {
      wx.hideLoading();
      console.log("发布求租：", res);
      if (res.data.status == "success") {
        wx.showToast({
          title: "编辑成功",
          icon: "none",
          mask: true,
          duration: 2000,
          success: function () {
            wx.redirectTo({
              url: "../car-rental/car-rental",
            });
          }
        });
      } else {
        wx.showToast({
          title: "编辑失败",
          icon: "none",
          mask: true,
          duration: 2000
        });
      }
    });
  }
});